- 振り返り
- なぜPythonでなくRを使うのか?
- Reproducible Research
- Tidy Data
- Data Science Workflow
- Program
- Import
- Tidy/Transform
- Visualize
- Communicate
- おまけ
2018年12月15日
Six Reasons To Learn R For Business, R Blogger
乱暴に言うと「Coddの第三正規形」を満たすデータ(対義語はMessy Data)。
日本語では以下となり、構造と意味が一致しているデータをTidy Dataと呼びます。
RStudioを利用する場合はR MarkdownのR Notebook機能を活用すると実結果を確認するためにknitする必要がなく分析が早く分かりやすくなります。
R Notebook機能を上手に活用するには[Ctrl + Enter]と[Ctrl + Shift + Enter]のショートカットキーを利用します。
コードがパイプでつながっている場合は、[Ctrl + Enter]でも一連のコードを実行してくれます。
iris
tidyr::gather関数は、keyに変数名をvalueにその値をまとめます。
iris %>% tidyr::gather(key = "Part", value = "Value", -Species) %>% head()
tidyr::spread関数はkeyを変数名にvalueをその値として展開します。
iris %>% tibble::rowid_to_column("id") %>%
tidyr::gather(key = "Part", value = "Value", -id, -Species) %>%
tidyr::spread(key = Part, value = Value) %>% dplyr::select(-id) %>% head()
iris %>% ggplot2::ggplot(ggplot2::aes(x = Species, y = Sepal.Width)) +
ggplot2::geom_boxplot(ggplot2::aes(colour = Species))
iris %>% dplyr::mutate(Species = forcats::fct_reorder(Species, Sepal.Width)) %>%
ggplot2::ggplot(ggplot2::aes(x = Species, y = Sepal.Width)) +
ggplot2::geom_boxplot(ggplot2::aes(colour = Species))
purrr::map関数は指定した引数に対して指定した処理(関数、演算子など)を適用する関数ですので、繰り返し処理に威力を発揮します。例えば、
file_path %>% list.files(path = ., pattern = "(issues_)", full.names = TRUE) %>% purrr::map_df(.x = ., .f = readr::read_csv, locale = readr::locale(encoding = "cp932"))
iris %>% split(.$Species) %>%
purrr::map_df(~ lm(Sepal.Length ~ Sepal.Width, data = .x) %>% broom::tidy(),
.id = "Species")
c関数はベクトル型変数やリスト型変数をcombine(結合)する関数。
c(issue_id = 16451L, list(id = 2L, name = "Resolution", value = "Invalid"))
## $issue_id ## [1] 16451 ## ## $id ## [1] 2 ## ## $name ## [1] "Resolution" ## ## $value ## [1] "Invalid"
第6回で説明したpurrr::map_df関数を使った カスタムフィールドの展開 は
with(issues, purrr::map_df(.x = custom_fields, .f = function(.x) {c(.x)}))
以下の処理と等価です(なお、c関数がなくても結果は同じです)。
dplyr::bind_rows( c(list(id = 2L, name = "Resolution", value = "Invalid")), c(list(id = 4L, name = "Affected version")) )
第6回で説明したpurrr::map2_df関数を使った カスタムフィールドの展開 は
with(issues, purrr::map2_df(.x = id, .y = custom_fields,
.f = function(.x, .y) {c(issue_id = .x, .y)}))
以下の処理と等価です。
dplyr::bind_rows( c(issue_id = 16451L, list(id = 2L, name = "Resolution", value = "Invalid")), c(issue_id = 16451L, list(id = 4L, name = "Affected version")) )
iris %>% ggplot2::ggplot(ggplot2::aes(x = Species, fill = Species)) + ggplot2::geom_boxplot(ggplot2::aes(y = Sepal.Length), alpha = 0.5)
iris %>% ggplot2::ggplot(ggplot2::aes(x = Species, fill = Species)) + ggplot2::geom_boxplot(ggplot2::aes(y = Sepal.Length, alpha = 0.5))
iris %>% ggplot2::ggplot(ggplot2::aes(x = Petal.Width, y = Petal.Length)) + ggplot2::geom_point(ggplot2::aes(colour = Species, size = Sepal.Length, shape = Species))
flexdashboradの区切りはMarkdownのHeader記述(#や##)で代用できます。幅は合計が1,000になるようにするといい塩梅です。メニューは#レベルを記述すれば自動的にメニューとなり、タブはカラムやローレベル(##)で明示的に指定する必要があります。 テンプレート例
# Menu (Tab)
## Coloumn Left {data-width=650}
### Component
## Coloumn Rigth {data-width=350} {.tabset}
### Tab 1
### Tab 2
htmlwidgets はHTMLドキュメントでJavaScriptを用いてインタラクティブな表示を提供するためのベースとなるパッケージです。htmlwidgetsパッケージを利用した様々なパッケージが公開されています。代表的なパッケージには以下のようなものがあります。
| package | description |
|---|---|
| DT | DataTablesライブラリを用いたテーブル(表)表示 |
| plotly | ggplot2オブジェクトを簡単にインタラクティブ化 |
| dygraphs | dygraphsライブライを用いた時系列グラフに特化したパッケージ |
mtcars %>% DT::datatable(options = list(pageLength = 5))
(iris %>% ggplot2::ggplot(ggplot2::aes(x = Petal.Width, y = Petal.Length)) +
ggplot2::geom_point(ggplot2::aes(colour = Species))) %>% plotly::ggplotly()
xts::as.xts(nhtemp) %>% dygraphs::dygraph(main = "New Haven Temperatures") %>% dygraphs::dyRangeSelector()
CC BY-NC-SA 4.0, Sampo Suzuki